// pages/checkIn/checkIn.js
var app = getApp();
var yangdate = require("sign_in_ss.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    signDay: [],
    signs: [],
    signtype: "1",
    signDays: [],
    todayDate: "1",
    todayMonth: "",
    todayYear: "",
    nextMonth: "",
    nextYear: "",
    prevYear: "",
    prevMonth: "",
    seriesCount: "99",
    series_gos: "15",
    for_signs: "none",
    total_count:0,
    is_checkin:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    //获取手机高度
    this.setData({
      navH: app.globalData.navHeight,
    });
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
        } else {
          // 未授权，跳转到授权页面
          wx.reLaunch({
            url: '/pages/zcuslist/index?partnerUserId=' + that.data.partnerUserId,
          })
        }
      }
    })
    wx.request({//获取积分
      url: app.globalData.url + 'getUserByUserId?userId=' + wx.getStorageSync("userId"),
      method:"get",
      success:function(res){
        var user = res.data.data;
        that.setData({
          points: user.points,
        })
        wx.setStorageSync("points", user.points)
      }
    })
    wx.request({//今天是否签到
      url: app.globalData.url + 'userSignInToday',
      method:"get",
      data:{
        userId: wx.getStorageSync("userId"),
      },
      success:function(res){
        if(res.data.result == 0){
          that.setData({
            is_checkin:true,
          })
        } else {
          that.setData({
            is_checkin: false,
          })
        }
      }
    });
    var getToday = new Date();
    var todayDate = getToday.getDate();
    var todayMonths = getToday.getMonth();
    var todayMonth = (todayMonths + 1);
    var todayYear = getToday.getFullYear();
    var todayss = getToday.getDate();
    if (todayMonth < 10) {
      var todayMonthss = "0" + todayMonth;
    } else {
      var todayMonthss = todayMonth;
    }
    wx.request({//获取累计签到天数
      url: app.globalData.url + 'userTotalDays',
      method: "get",
      data: {
        userId: wx.getStorageSync("userId"),
        dateStr: '' + todayYear + '-' + todayMonthss,
      },
      success: function(res){
        var resList = res.data.data.length;
        that.setData({
          total_count:resList,
        })
      }
    });
    wx.request({
      url: app.globalData.url + 'userSignInDays',
      method:"get",
      data:{
        userId: wx.getStorageSync("userId"),
      },
      success:function(res){
        var maxDays = res.data.data;
        that.setData({
          thousands:parseInt(maxDays/100),
          hundreds:parseInt(maxDays%100/10),
          numbers:parseInt(maxDays%10),
        })
      }
    })
    console.log(todayss);
    var godates = todayYear + "/" + todayMonthss + "/01";
    wx.request({
      url: app.globalData.url + 'userSignInList',
      data:{
        userId: wx.getStorageSync("userId"),
      },
      method:"get",
      success:function(res){
        var requestList = res.data.data;
        var daysArr = [];
        for (var i = 0; i < requestList.length; i++){
          var dataStr = requestList[i].clockInTime.replace(/\.|\-/g, "/");
          var singDay = new Date(dataStr).getDate();
          var signMonth = new Date(dataStr).getMonth() + 1;
          if (signMonth == parseInt(todayMonth)){//获取当前月的签到天数
            daysArr.push(singDay);
          }
        }
        var $datas = { seriesCount: 1, signDays: daysArr };
        var signDate_arr = new Array();
        var anns = $datas.signDays;
        var count_signday = $datas.seriesCount;
        if (count_signday > 9) {
          var series_gos = "0";
        } else if (count_signday < 0) {
          var series_gos = 99;
        } else {
          var series_gos = 10 - parseInt(count_signday);
        }
        that.setData({
          seriesCount: count_signday,
          series_gos: series_gos,
        });
        console.info($datas.signDays);
        for (var p in anns) {//遍历json对象的每个key/value对,p为key
          var newdats = anns[p];
          signDate_arr.push(newdats);
        }
        if (signDate_arr.indexOf(todayss) > -1) {
          console.log("当前已签到");
          that.setData({
            signtype: "2",
          });
        } else {
          console.log("当前未签到");
          that.setData({
            signtype: "1",
          });
        }
        console.log(signDate_arr[0]);
        yangdate.yang_date.bulidCal(todayYear, todayMonth, that, signDate_arr);
      }      
    });
    this.setData({
      todayDate: todayDate,
      todayMonth: todayMonth,
      todayYear: todayYear,
      prevYear: todayYear,
      nextYear: todayYear,
      prevMonth: todayMonth,
      nextMonth: todayMonth,
      showYear: todayYear,
      showMonth: todayMonth,
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  signStart: function () {
    var that = this;
    wx.request({
      url: app.globalData.url + 'userSignIn?userId=' + wx.getStorageSync("userId"),
      method:"post",
      success:function(res){
        if(res.data.result == 1){
          //签到成功后重新调用后台接口加载新的签到数据
          that.setData({
            for_signs: "block",
            signtype: "2",
          });
          console.info(res.data.data.awardPoint);
          if (res.data.data.awardCoupon > 0) {
            wx.showToast({
              title: '签到成功,奖励优惠券',
              icon: 'none',
              duration: 3000
            });
          } else  if (res.data.data.awardPoint > 0) {
            wx.showToast({
              title: '签到成功,奖励积分' + res.data.data.awardPoint+'分',
              icon: 'none',
              duration: 3000
            });
          } else {
            wx.showToast({
              title: '签到成功' ,
              icon: 'success',
              duration: 3000
            });
          }
          
          that.onLoad();
        }
      }
    })
    
  },

  sign_prev: function () {
    console.log("上一月");
    var showMonth = this.data.showMonth;
    var todayMonth = this.data.todayMonth;
    if (showMonth == "1") {
      var showMonth = "12";
      var showYear = parseInt(this.data.showYear) - 1;
    } else {
      var showMonth = parseInt(this.data.showMonth) - 1;
      var showYear = this.data.showYear;
    }
    if (parseInt(todayMonth - 3) == showMonth) {
      wx.showToast({
        title: '不能查看更多了',
        icon: 'loading',
        duration: 1500
      });
      return;
    }
    var that = this;
    if (showMonth < 10) {
      var showMonths = "0" + showMonth;
    } else {
      var showMonths = todayMonth;
    }
    var godates = showYear + "/" + showMonths + "/01";
    wx.request({
      url: app.globalData.url + 'userSignInList',
      data: {
        userId: wx.getStorageSync("userId"),
      },
      method: "get",
      success: function (res) {
        var requestList = res.data.data;
        var daysArr = [];
        for (var i = 0; i < requestList.length; i++) {
          var dataStr = requestList[i].clockInTime.replace(/\.|\-/g, "/");
          var singDay = new Date(dataStr).getDate();
          var signMonth = new Date(dataStr).getMonth() + 1;
          var signYear = new Date(dataStr).getFullYear();
          if (signMonth == parseInt(showMonths) && signYear == parseInt(showYear)) {//获取当前月的签到天数
            daysArr.push(singDay);
          }
        }
        var $datas = { seriesCount: 1, signDays: daysArr };
        var signDate_arr = new Array();
        var anns = $datas.signDays;
        var count_signday = $datas.seriesCount;
        if (count_signday > 9) {
          var series_gos = "0";
        } else if (count_signday < 0) {
          var series_gos = 99;
        } else {
          var series_gos = 10 - parseInt(count_signday);
        }
        that.setData({
          seriesCount: count_signday,
          series_gos: series_gos,
        });
        console.info($datas.signDays);
        for (var p in anns) {//遍历json对象的每个key/value对,p为key
          var newdats = anns[p];
          signDate_arr.push(newdats);
        }
        console.log(signDate_arr[0]);
        yangdate.yang_date.bulidCal(showYear, showMonth, that, signDate_arr);
      }
    });
    //初始化加载日历
    this.setData({
      showYear: showYear,
      showMonth: showMonth,
    });
  },
  sign_next: function () {
    console.log("下一月");
    var showMonth = this.data.showMonth;
    var todayMonth = this.data.todayMonth;
    if (todayMonth == showMonth) {
      wx.showToast({
        title: '未签到不能查看',
        icon: 'loading',
        duration: 1500
      });
      return;
    }
    if (showMonth == "12") {
      var showMonth = "1";
      var showYear = parseInt(this.data.showYear) + 1;
    } else {
      var showMonth = parseInt(this.data.showMonth) + 1;
      var showYear = this.data.showYear;
    }
    var that = this;
    if (showMonth < 10) {
      var showMonths = "0" + showMonth;
    } else {
      var showMonths = todayMonth;
    }
    var godates = showYear + "/" + showMonths + "/01";
    wx.request({
      url: app.globalData.url + 'userSignInList',
      data: {
        userId: wx.getStorageSync("userId"),
      },
      method: "get",
      success: function (res) {
        var requestList = res.data.data;
        var daysArr = [];
        for (var i = 0; i < requestList.length; i++) {
          var dataStr = requestList[i].clockInTime.replace(/\.|\-/g, "/");
          var singDay = new Date(dataStr).getDate();
          var signMonth = new Date(dataStr).getMonth() + 1;
          var signYear = new Date(dataStr).getFullYear();
          if (signMonth == parseInt(showMonths) && signYear == parseInt(showYear)) {//获取当前月的签到天数
            daysArr.push(singDay);
          }
        }
        var $datas = { seriesCount: 1, signDays: daysArr };
        var signDate_arr = new Array();
        var anns = $datas.signDays;
        var count_signday = $datas.seriesCount;
        if (count_signday > 9) {
          var series_gos = "0";
        } else if (count_signday < 0) {
          var series_gos = 99;
        } else {
          var series_gos = 10 - parseInt(count_signday);
        }
        that.setData({
          seriesCount: count_signday,
          series_gos: series_gos,
        });
        console.info($datas.signDays);
        for (var p in anns) {//遍历json对象的每个key/value对,p为key
          var newdats = anns[p];
          signDate_arr.push(newdats);
        }
        console.log(signDate_arr[0]);
        yangdate.yang_date.bulidCal(showYear, showMonth, that, signDate_arr);
      }
    });
    //初始化加载日历
    this.setData({
      showYear: showYear,
      showMonth: showMonth,
    });
  },

  // getCurrentMonthDayNum: function (date) {//:   yyyy/MM/dd
  //   let dayAllThisMonth = 31;
  //   if (date.getMonth() + 1 != 12) {
  //     let currentMonthStartDate = new Date(date.getFullYear() + "/" + (date.getMonth() + 1) + "/01"); // 本月1号的日期
  //     let nextMonthStartDate = new Date(date.getFullYear() + "/" + (date.getMonth() + 2) + "/01"); // 下个月1号的日期
  //     dayAllThisMonth = (nextMonthStartDate - currentMonthStartDate) / (24 * 3600 * 1000);
  //   }
  //   return dayAllThisMonth;
  // },
  toHis:function(){
    wx.navigateTo({
      url: '/pages/pointHistory/pointHistory',
    })
  },
  navBack: function () {
    wx.navigateBack({

    })
  },
  toRule: function () {
    wx.navigateTo({
      url: '/pages/webView/webView?url=' + app.globalData.url + 'getWebPage&webId=pointRules',
    })
  },
})